package com.zHan.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zHan.model.system.SysDept;
import com.zHan.model.system.SysPost;
import com.zHan.model.system.SysUser;
import com.zHan.model.vo.RoleCountVo;
import com.zHan.system.mapper.SysDeptMapper;
import com.zHan.system.mapper.SysPostMapper;
import com.zHan.system.mapper.SysRoleMapper;
import com.zHan.system.mapper.SysUserMapper;
import com.zHan.system.service.ICountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: CountServiceImpl
 * Description:
 * date: 2024/1/10 0010 15:38
 *
 * @author zHan
 */
@Service
@Slf4j
public class CountServiceImpl implements ICountService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private SysPostMapper postMapper;

    /**
     * 统计各自的数量
     *
     * @return
     */
    @Override
    public Map<String, Object> getAllQuantity() {
        // 创建返回结果
        Map<String, Object> map = new HashMap<>();
        // 用户数量
        Integer userCount = userMapper.selectCount(Wrappers.<SysUser>lambdaQuery()
                .eq(SysUser::getStatus, 1));
        // 角色数量
        Integer roleCount =
                roleMapper.selectCount(null);
        // 部门数量
        Integer deptCount = deptMapper.selectCount(Wrappers.<SysDept>lambdaQuery()
                .eq(SysDept::getStatus, 1));
        // 岗位数量
        Integer postCount = postMapper.selectCount(Wrappers.<SysPost>lambdaQuery()
                .eq(SysPost::getStatus, 1));
        // 添加到集合
        map.put("userCount", userCount);
        map.put("roleCount", roleCount);
        map.put("deptCount", deptCount);
        map.put("postCount", postCount);
        return map;
    }

    /**
     * 男女数量
     *
     * @return
     */
    @Override
    public Map<String, Object> getSexCount() {
        // 创建返回结果
        Map<String, Object> map = new HashMap<>();
        // 男用户数量
        Integer maleCount = userMapper.selectCount(Wrappers.<SysUser>lambdaQuery()
                .eq(SysUser::getSex, 1)
                .eq(SysUser::getStatus, 1));

        // 女用户数量
        Integer femaleCount = userMapper.selectCount(Wrappers.<SysUser>lambdaQuery()
                .eq(SysUser::getSex, 0)
                .eq(SysUser::getStatus, 1));
        // 添加到集合
        map.put("maleCount", maleCount);
        map.put("femaleCount", femaleCount);
        return map;
    }

    /**
     * 已分配角色的数量
     *
     * @return
     */
    @Override
    public Map<String, Object> getRoleCount() {
        // 创建返回结果
        Map<String, Object> map = new HashMap<>();
        // 准备集合数据
        List<String> roleNameList = new ArrayList<>();
        List<Integer> roleCountList = new ArrayList<>();

        // 查询已分配角色的数量
        List<RoleCountVo> roleCountVoList = roleMapper.selectRoleCount();
        for (RoleCountVo roleCountVo : roleCountVoList) {
            roleNameList.add(roleCountVo.getRoleName());
            roleCountList.add(roleCountVo.getRoleCount());
        }
        // 添加到map集合
        map.put("roleNameList", roleNameList);
        map.put("roleCountList", roleCountList);
        return map;
    }
}
